package com.hm.PushCaseService.service.ydcg.caseinfo.help;

import com.hm.PushCaseService.constant.ReqUrl;
import com.hm.PushCaseService.pojo.CaseBean;
import com.hm.PushCaseService.pojo.CaseRespBean;
import com.hm.PushCaseService.service.caseinfo.ICase;
import com.hm.PushCaseService.service.timestamp.ICaseLastUpdateTime;
import com.hm.PushCaseService.service.ydcg.authentication.IAuthentication;
import com.hm.PushCaseService.service.ydcg.caseinfo.info.ICaseInfo;
import com.hm.PushCaseService.service.ydcg.caseinfo.replenish.IReplenishCase;
import com.hm.PushCaseService.utils.time.DateTimeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取案件信息
 */

@Service
public class CaseHelpService implements ICaseHelp {

    private static final Logger logger = LoggerFactory.getLogger(CaseHelpService.class);

    //鉴权
    private final IAuthentication authentication;
    //时间戳
    private final ICaseLastUpdateTime caseLastUpdateTime;
    //获取案件信息
    private final ICaseInfo iCaseInfo;
    //补充信息-专业部门案件
    private final IReplenishCase iReplenishCase;
    //案件服务
    private final ICase iCase;

    public CaseHelpService(ICase iCase, ICaseInfo iCaseInfo, IAuthentication authentication, ICaseLastUpdateTime caseLastUpdateTime, IReplenishCase iReplenishCase) {
        this.iCaseInfo = iCaseInfo;
        this.authentication = authentication;
        this.caseLastUpdateTime = caseLastUpdateTime;
        this.iReplenishCase = iReplenishCase;
        this.iCase = iCase;
    }

    @Override
    public List<CaseBean> getSelfDispCaseList() {
        //token
        String token = authentication.getToken();
        //最后更新时间
        String lastUpdateTimeTime = caseLastUpdateTime.getTime();
        //当前时间
        String currentTime = DateTimeUtils.getCurrentTime();
        //ReqPatrolListUrl：自行处理全量接口
        CaseRespBean caseInfo = iCaseInfo.getCaseInfo(lastUpdateTimeTime, currentTime, 1, token, ReqUrl.ReqPatrolListUrl);
        //有几页就循环请求几次-->一次性拉取
        int totalPages = caseInfo.totalPages;
        logger.warn("自行处理案件共有" + caseInfo.totalCount + "条，共有" + totalPages + "页");
        List<CaseBean> result = caseInfo.result;
        if (result == null || result.isEmpty()) {
            return new ArrayList<>();
        }
        List<CaseBean> caseList = new ArrayList<>(result);
        for (int i = 2; i <= totalPages; i++) {
            //第二页开始
            CaseRespBean caseRespBean = iCaseInfo.getCaseInfo(lastUpdateTimeTime, currentTime, i, token, ReqUrl.ReqPatrolListUrl);
            caseList.addAll(caseRespBean.result);
        }
        return caseList;
    }

    @Override
    public List<CaseBean> getProfessionCaseList() {
        //token
        String token = authentication.getToken();
        //最后更新时间
        String lastUpdateTimeTime = caseLastUpdateTime.getTime();
        //当前时间
        String currentTime = DateTimeUtils.getCurrentTime();
        //ReqPatrolListUrl：自行处理全量接口
        CaseRespBean caseInfo = iCaseInfo.getCaseInfo(lastUpdateTimeTime, currentTime, 1, token, ReqUrl.ReqCaseListUrl);
        //有几页就循环请求几次-->一次性拉取
        int totalPages = caseInfo.totalPages;
        logger.warn("专业部门处理案件共有" + caseInfo.totalCount + "条，共有" + totalPages + "页");
        List<CaseBean> result = caseInfo.result;
        if (result == null || result.isEmpty()) {
            return new ArrayList<>();
        }
        List<CaseBean> caseList = new ArrayList<>(result);
        for (int i = 2; i <= totalPages; i++) {
            //第二页开始
            CaseRespBean caseRespBean = iCaseInfo.getCaseInfo(lastUpdateTimeTime, currentTime, i, token, ReqUrl.ReqCaseListUrl);
            caseList.addAll(caseRespBean.result);
        }
        //补充案件信息
        for (CaseBean caseBean : caseList) {
            iReplenishCase.replenish(caseBean);
        }
        return caseList;
    }

    @Override
    public void insertBatch(List<CaseBean> caseList) {
        //自行处置
        iCase.addBatchCase(caseList);
    }

    @Override
    public void updateTime() {
        //当前时间
        String currentTime = DateTimeUtils.getCurrentTime();
        caseLastUpdateTime.updateTime(currentTime);
    }


}
